package com.yanqu.road.server.manger.config;

import com.yanqu.road.entity.config.promotion.PromotionInfo;
import com.yanqu.road.logic.bussiness.config.PromotionInfoBusiness;
import com.yanqu.road.server.TempMgr;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PromotionMgr extends TempMgr {

    //晋升角色id，晋升信息
    private static Map<Integer, List<PromotionInfo>> promotionInfoMap = new ConcurrentHashMap<>();
    private static Map<Integer, PromotionInfo> promotionInfoMapById = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reload() throws Exception {
        return super.reload();
    }

    public static PromotionInfo getPromotionInfoById(int promotionId) {
        return promotionInfoMapById.get(promotionId);
    }

    public static Map<Integer, List<PromotionInfo>> getPromotionInfoMap() {
        return promotionInfoMap;
    }

    public static List<PromotionInfo> getCharacterPromotion(int characterId) {
        return promotionInfoMap.get(characterId);
    }

    public static PromotionInfo getCharacterNowPromotion(int characterId, int promotionId) {
        if (promotionId == 0) {//表示没晋升过
            return null;
        }

        List<PromotionInfo> promotionInfos = getCharacterPromotion(characterId);
        if (promotionInfos == null || promotionInfos.isEmpty()) {
            return null;
        }

        for (PromotionInfo promotionInfo : promotionInfos) {
            if (promotionInfo.getId() == promotionId) {
                return promotionInfo;
            }
        }

        return null;
    }

    public static PromotionInfo getCharacterNextPromotion(int characterId, int nowPromotionId) {
        List<PromotionInfo> promotionInfos = getCharacterPromotion(characterId);
        if (promotionInfos == null || promotionInfos.isEmpty()) {
            return null;
        }

        if (nowPromotionId == 0) {//没晋升过取第一条
            return promotionInfos.get(0);
        }

        long nextPromotionId = 0;
        for (PromotionInfo promotionInfo : promotionInfos) {
            if (promotionInfo.getId() == nowPromotionId) {
                if (promotionInfo.getNextId() > 0) {
                    nextPromotionId = promotionInfo.getNextId();
                }
            }

            if (promotionInfo.getId() == nextPromotionId) {
                return promotionInfo;
            }
        }

        return null;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        Map<Integer, PromotionInfo> tmpMapById = new HashMap<>();
        Map<Integer, List<PromotionInfo>> tmpMap = PromotionInfoBusiness.getPromotionInfoList();
        for (List<PromotionInfo> promotionInfoList : tmpMap.values()) {
            promotionInfoList.sort(Comparator.comparing(PromotionInfo::getId));

            for (PromotionInfo promotionInfo : promotionInfoList) {
                tmpMapById.put(promotionInfo.getId(), promotionInfo);
            }
        }
        promotionInfoMap = tmpMap;
        promotionInfoMapById = tmpMapById;
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }
}
